﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DefeatEpidemic.Common;
using DefeatEpidemic.Models;
using DefeatEpidemic.RequestModels;
using Microsoft.AspNetCore.Mvc;


// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace DefeatEpidemic.API
{
    [Route("api/[controller]/[action]")]
    [GlobalActionFilter]
    public class MemberController : Controller
    {
        private readonly DefeatEpidemicContext _context;

        public MemberController(DefeatEpidemicContext context)
        {
            _context = context;
        }
        /// <summary>
        /// 注册绑定用户
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response> BindMember([FromBody]MemberModel model)
        {
            Response resp = new Response();
            try
            {
                if (ModelState.IsValid)
                {
                    if (_context.Members.Count(u => u.idcard == model.idcard) > 0)
                    {
                        resp.code = -1;
                        resp.message = "该用户已存在";
                    }
                    else
                    {
                        var community = _context.Community.Where(u => u.labelNumber == model.code).FirstOrDefault();
                        var member = new Member()
                        {
                            name = model.name,
                            idcard = model.idcard,
                            mobile = model.mobile,
                            address = model.houseNumber,
                            carNumber = model.carNumber,
                            community = community,
                            role_type = 1
                        };
                        _context.Add(member);
                        var oauth = _context.Wxoauths.Where(u => u.open_id == model.open_id).First();
                        oauth.member = member;
                        oauth.type = member.role_type;
                        _context.Update(oauth);
                        int ret = await _context.SaveChangesAsync();
                        if (ret > 0)
                        {
                            resp.code = 1;
                            resp.message = "success";
                            resp.data = new { member_id = member.id };
                        }
                    }
                }
                else
                {
                    resp.code = -1;
                    resp.message = "模型格式错误";
                }
            }
            catch(Exception ex)
            {
                resp.code = -1;
                resp.message = $"error:{ex.Message},{ex.StackTrace}";
            }
            return resp;
        }

        /// <summary>
        /// 绑定管理员
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response> BindMamager([FromBody]ManagerModel model)
        {
            Response resp = new Response();
            try
            {
                var manager = _context.Members.Where(u => u.role_type == 2 && u.mobile == model.mobile).FirstOrDefault();
                if (manager != null)
                {
                    var oauth = _context.Wxoauths.Where(u => u.open_id == model.open_id).FirstOrDefault();
                    oauth.member = manager;
                    oauth.type = manager.role_type;
                    _context.Update(oauth);
                    int ret = await _context.SaveChangesAsync();
                    if (ret > 0)
                    {
                        resp.code = 1;
                        resp.message = "绑定成功";
                        resp.data = new { member_id = manager.id };
                    }
                }
                else
                {
                    resp.message = "请先联系社区管理人员添加手机号";
                }
            }
            catch (Exception ex)
            {
                resp.code = -1;
                resp.message = $"error:{ex.Message},{ex.StackTrace}";
            }
            return resp;
        }

        /// <summary>
        /// 获取个人信息
        /// </summary>
        /// <param name="member_id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response> GetMemberInfo(Guid member_id)
        {
            Response resp = new Response();
            try
            {
                var member = _context.Members.Find(member_id);
                await _context.Entry(member).Reference(d => d.community).LoadAsync();
                resp.code = 1;
                resp.data = new { member.name, member.idcard, member.mobile, member.address, member.carNumber, community_name = member.community.name };
            }
            catch(Exception ex)
            {
                resp.code = -1;
                resp.message = $"error:{ex.Message}.{ex.StackTrace}";
            }
            return resp;
        }        


    }
}
